﻿using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Adams.Public.DataAccess.Web.DailyPrayers
{
    public static class Index
    {
        public static Adams.Public.DataTransfer.Web.DailPrayers.Index.PageLoad PageLoad(string location, int monthId, bool isHijri)
        {
            var data = new Adams.Public.DataTransfer.Web.DailPrayers.Index.PageLoad();
            data.PrayerTimes = new Shared.Entities.DailyPrayers.PrayerTimeCollection();
            data.Locations = new Shared.Entities.DailyPrayers.LocationCollection();

            data.Month = monthId;

            using (SqlDataReader dr = DB.RunDataReader(@"
DECLARE @LocationID int
DECLARE @InheritMaster bit

IF(@Location = '0')
BEGIN
    SELECT @LocationID = LocationID, @InheritMaster = 0 FROM DailyPrayers.Locations WHERE IsMaster = 1
END
ELSE
BEGIN
    SELECT @LocationID = LocationID, @InheritMaster = InheritMaster FROM DailyPrayers.Locations WHERE Name = @Location
END

SELECT LocationID, Name
FROM DailyPrayers.Locations
WHERE LocationID = @LocationID

IF(@InheritMaster = 1)
BEGIN
    
    IF (@IsHijri = 1)
    BEGIN
        SELECT A.*
        FROM DailyPrayers.PrayerTimes A
        WHERE LocationID = (SELECT LocationID FROM DailyPrayers.Locations WHERE IsMaster = 1) AND IMonth = @MonthID
        ORDER BY IMonth, IDay
    END
    ELSE
    BEGIN
        SELECT A.*
        FROM DailyPrayers.PrayerTimes A
        WHERE LocationID = (SELECT LocationID FROM DailyPrayers.Locations WHERE IsMaster = 1) AND Month = @MonthID
        ORDER BY Month, Day
    END
END
ELSE
BEGIN
    IF (@IsHijri = 1)
    BEGIN
        SELECT A.*
        FROM DailyPrayers.PrayerTimes A
        WHERE LocationID = @LocationID AND IMonth = @MonthID
        ORDER BY IMonth, IDay
    END
    ELSE
    BEGIN
        SELECT A.*
        FROM DailyPrayers.PrayerTimes A
        WHERE LocationID = @LocationID AND Month = @MonthID
        ORDER BY Month, Day
    END
END

SELECT * 
FROM DailyPrayers.Locations
ORDER BY DisplayOrder
", CommandType.Text,
    DB.CreateParameter("@Location", location),
    DB.CreateParameter("@MonthID", monthId),
    DB.CreateParameter("@IsHijri", isHijri)))
            {
                dr.Read();
                data.LocationID = dr["LocationID"].DbInt();
                data.LocationName = dr["Name"].DbString();

                dr.NextResult();
                while (dr.Read())
                {
                    var prayer = new Shared.Entities.DailyPrayers.PrayerTime();
                    prayer.ID = dr["PrayerID"].DbInt();
                    prayer.Month = dr["Month"].DbInt();
                    prayer.Day = dr["Day"].DbInt();
                    prayer.IMonth = dr["IMonth"].DbInt();
                    prayer.IDay = dr["IDay"].DbInt();
                    prayer.Fajr = dr["Fajr"].DbString();
                    prayer.FajrIqama = dr["FajrIqama"].DbString();
                    prayer.Sunrise = dr["Sunrise"].DbString();
                    prayer.Duhr = dr["Duhr"].DbString();
                    prayer.DuhrIqama = dr["DuhrIqama"].DbString();
                    prayer.Asr = dr["Asr"].DbString();
                    prayer.AsrIqama = dr["AsrIqama"].DbString();
                    prayer.Maghrib = dr["Maghrib"].DbString();
                    prayer.MaghribIqama = dr["MaghribIqama"].DbString();
                    prayer.Isha = dr["Isha"].DbString();
                    prayer.IshaIqama = dr["IshaIqama"].DbString();
                    data.PrayerTimes.Add(prayer);
                }

                dr.NextResult();
                while (dr.Read())
                {
                    var loc = new Shared.Entities.DailyPrayers.Location();
                    loc.ID = dr["LocationID"].DbInt();
                    loc.IsMaster = dr["IsMaster"].DbBool();
                    loc.InheritMaster = dr["InheritMaster"].DbBool();
                    loc.Name = dr["Name"].DbString();
                    loc.Street = dr["Street"].DbString();
                    loc.Street2 = dr["Street2"].DbString();
                    loc.City = dr["City"].DbString();
                    loc.State = dr["State"].DbString();
                    loc.ZipCode = dr["ZipCode"].DbString();
                    loc.Latitude = dr["Latitude"].DbDecimal();
                    loc.Longitude = dr["Longitude"].DbDecimal();
                    loc.TimeZone = dr["TimeZone"].DbInt();
                    loc.LongNote = dr["LongNote"].DbString();
                    loc.DisplayOrder = dr["DisplayOrder"].DbInt();
                    data.Locations.Add(loc);
                }
                return data;
            }
        }
    }
}